<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Perfil extends CI_Controller {
    	
    private $PERFIL;
    
    function Perfil (){
        parent::__construct();
		
		if(!$this->usercontrol->has_permission('perfil')){
            redirect('home');
		}
        
        if(!$this->session->userdata('logged')){
        	redirect('login');
        }
 
        $this->PERFIL = array(
            0 => 'Administrador',
            1 => 'Atendente',
        );
    }
	
	public function index (){
    	$this->load->model('user_model');
	    $data = $this->user_model->get($this->session->userdata('user'));
	 			
	    $data['page_title']  = "Meu perfil";
	    $data['perfil_list'] = $this->PERFIL;
	 
	    // Load View
	    $this->template->show('perfil/edit', $data);
	}
	
	public function save(){
        if($this->input->post('cancel') !== FALSE){
        	redirect('usuario');
        }
            
        $user_id = $this->input->post('id');
        
        $this->load->library('form_validation');
		
        $this->form_validation->set_rules('nome', 'Nome', 'required|ucwords');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('username', 'Login', 'trim|required');
        $this->form_validation->set_rules('password', 'Senha', 'trim|required');
        
        if($this->form_validation->run() === false)  {
            $this->error = true;
            $this->index ();
			
            return;
        }
		
        $this->load->model('user_model');
        
        $sql_data = array(
        	'nome'		=> $this->input->post('nome'),
            'username'	=> $this->input->post('username'),
            'email'		=> $this->input->post('email'),
            'password'	=> $this->input->post('password'),
        );
        
    	$this->user_model->update($this->session->userdata('user'),$sql_data);
        redirect('usuario');
    }
 
}